//
//  LBMinusPlusView.m
//  loveBeen
//
//  Created by 韩天助 on 11/20/16.
//  Copyright © 2016 loveBeen. All rights reserved.
//

#import "LBMinusPlusView.h"
#import "LBPlusMinusTool.h"
#import "LBNotEnouchView.h"

@interface LBMinusPlusView ()<UIAlertViewDelegate>



@end

@implementation LBMinusPlusView
//显示数据
#pragma mark - 设置 减号 / label 是否隐藏 . 显示添加的数量
-(void)setModel:(LBSuperMarketProducts *)model{
    
    _model = model;
    
    _buyNumLabel.text = @(model.totalCount).description;
    
    if (model.isAnimation) {
        // 隐藏 ?
        _minusBtn.hidden = (model.totalCount == 0);
        
        _buyNumLabel.hidden = (model.totalCount == 0);
    }
    
}

#pragma mark - 监听加号按钮
- (IBAction)plusBtnClick:(id)sender {
    
    //  当添加数量 == 库存数量 时 提示库存不足, 不再添加,直接返回
    if (self.model.totalCount == self.model.store_nums.integerValue) {
        
        [self alertView];
        
        return;
    }
    
    self.model.totalCount++;
    
    
    self.model.selected = YES;

    
    // 代理
    if ([_delegate respondsToSelector:@selector(minusPlusView:)]) {
        [_delegate minusPlusView:self];
    }

    
    // 传递数据单列 --  统计选中各商品的 数量
    LBPlusMinusTool *manager = [LBPlusMinusTool sharedPlusMinusTool];

    if (![manager.selectedProductsModel containsObject:self.model]) {
        
        [manager.selectedProductsModel addObject:self.model];
    }
    
    [[LBPlusMinusTool sharedPlusMinusTool]fastUpdate:self];
    
    //执行代理方法 调用 set  方法 时,
    manager.change = YES;
    
    _minusBtn.hidden = (self.model.totalCount == 0);
    
    _buyNumLabel.hidden = (self.model.totalCount == 0);
    
    _buyNumLabel.text = @(self.model.totalCount).description;
    
    if ([_delegate respondsToSelector:@selector(minusPlusView:)]) {
        [_delegate minusPlusView:self];
    }
    
    // 通知
    if (self.model.isAnimation) {
        
        NSDictionary *dict = @{@"img":self.iconImage,@"mpView":self.superview};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:Animate object:self userInfo:dict];
    }

}
#pragma mark - 添加提示框
- (void)alertView{

    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    LBNotEnouchView *alertView = [[LBNotEnouchView alloc]initWithName:self.model.name];
    [keyWindow addSubview:alertView];
    
    // alertView 的约束
    [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(80);
        make.right.offset(-80);
        make.top.equalTo(keyWindow).offset(230);
        make.height.mas_equalTo(150);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [alertView removeFromSuperview];
    });

}

#pragma mark - 监听减号按钮
- (IBAction)minusBtnClick:(id)sender {
    
    if (self.model.totalCount == 0) {
        
    }
    else if((!self.model.isAnimation)&&(self.model.totalCount ==1)){
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"确定要删除该商品吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
    else{
        self.model.totalCount--;
        self.model.selected = YES;
        
        LBPlusMinusTool *manager = [LBPlusMinusTool sharedPlusMinusTool];
        
        
        if (self.model.totalCount == 0) {
            
            [manager.selectedProductsModel removeObject:self.model];
            
        }
        [[LBPlusMinusTool sharedPlusMinusTool]fastUpdate:self];
        
        if ([_delegate respondsToSelector:@selector(minusPlusView:)]) {
            [_delegate minusPlusView:self];
        }
        
        //随意值执行代理方法
        manager.change = NO;
        
        _buyNumLabel.text = @(self.model.totalCount).description;
        
        if (self.model.isAnimation) {
            
            _minusBtn.hidden = (self.model.totalCount == 0);
            
            _buyNumLabel.hidden = (self.model.totalCount == 0);
            
        }
        
    }
}


//alert代理方法减号专用

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex ==0) {
        return;
    }
    else if(buttonIndex ==1){
        
        self.model.totalCount--;
        self.model.selected = YES;
        
        LBPlusMinusTool *manager = [LBPlusMinusTool sharedPlusMinusTool];
        
        
        if (self.model.totalCount == 0) {
            
            [manager.selectedProductsModel removeObject:self.model];
            
            if(!manager.selectedProductsModel.count)
            {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"PushEmpty" object:self];
        
            }
        }
        [[LBPlusMinusTool sharedPlusMinusTool]fastUpdate:self];
        
        
        
        //随意值执行代理方法
        manager.change = NO;
        
        _buyNumLabel.text = @(self.model.totalCount).description;
        
        if (self.model.isAnimation) {
            
            _minusBtn.hidden = (self.model.totalCount == 0);
            
            _buyNumLabel.hidden = (self.model.totalCount == 0);
            
         

        }
    }
    
}



//清除方法

@end
